package primary.code06_graph;

import java.util.ArrayList;
import java.util.HashMap;

public class GraphUtils {
    public static Graph transferGraph(int[][] arr) {
        final Graph graph = new Graph(new HashMap<>(), new ArrayList<>());
        for (int[] ints : arr) {
            if (!graph.nodes.containsKey(ints[1])) {
                graph.nodes.put(ints[1], new Node(ints[1]));
            }
            if (!graph.nodes.containsKey(ints[2])) {
                graph.nodes.put(ints[2], new Node(ints[2]));
            }
            Node from = graph.nodes.get(ints[1]);
            Node to = graph.nodes.get(ints[2]);
            Edge edge = new Edge(from, to, ints[0]);
            graph.edges.add(edge);
            from.out++;
            to.in++;
            from.edges.add(edge);
            from.next.add(to);
        }
        return graph;
    }

    public static Graph transferGraphUndirected(int[][] arr) {
        final Graph graph = new Graph(new HashMap<>(), new ArrayList<>());
        for (int[] ints : arr) {
            if (!graph.nodes.containsKey(ints[1])) {
                graph.nodes.put(ints[1], new Node(ints[1]));
            }
            if (!graph.nodes.containsKey(ints[2])) {
                graph.nodes.put(ints[2], new Node(ints[2]));
            }
            Node from = graph.nodes.get(ints[1]);
            Node to = graph.nodes.get(ints[2]);
            Edge edge1 = new Edge(from, to, ints[0]);
            Edge edge2 = new Edge(to, from, ints[0]);
            graph.edges.add(edge1);
            graph.edges.add(edge2);
            from.out++;
            from.in++;
            to.out++;
            to.in++;
            from.edges.add(edge1);
            to.edges.add(edge2);
            from.next.add(to);
            to.next.add(from);
        }
        return graph;
    }
}
